
/****************************************************************************
* 			Title: STYL Paper MAIN TABLES
*			Output:
*			Date:
*****************************************************************************/

////1. DIRECTORY INFORMATION ////
clear all
set maxvar 30000

////2. ADDITIONAL SET UP ////
set varabbrev off
set matsize 1000


insheet using "data/prev/SDV_Data.csv", clear names comma
	 sort partid p2_e2w2w p2_e13m p2_e12m p2_e2wks p1_e7m p1_e5m p1_e12m p1_e13m p3_e12m p3_e13m
	 gen sdv_conducted = 1
     destring sdv_gambl sdv_video, ignore("99-Missing") replace


	 tempfile sdvdata
	 save "`sdvdata'"


use "data/clean/STYL_lr_panel_analysis.dta", clear


//// 3. SURVEYSET, PROGRAMS AND GLOBALS ////

 	//c. SET SURVEY
qui include dofiles/STYL_longrun_globals.do
sort partid round
svyset, strata(strata) psu(partid)


    * Label Treatment arms
			qui la var tpassonly "Therapy Only"
			qui la var cashassonly "Cash Only"
			qui la var tpcashass "Therapy + Cash"

		/* Generate fixed effects dummies */
			qui tab tp_strata_alt, g(tp_dummy)
			qui tab cg_strata, g(cg_dummy)
			qui tab neighborhoodphase, g(nbhd_dummy)

			gl cohorts "redlight_p1 redlight_p2 centralmonrovia_p2 redlight_p3 centralmonrovia_p3 claratown_p3 logantown_p3 newkrutown_p3 "


        cap drop __000001
        cap drop __000000


	 sort partid p2_e2w2w p2_e13m p2_e12m p2_e2wks p1_e7m p1_e5m p1_e12m p1_e13m p3_e12m p3_e13m
	 merge n:1 partid p2_e2w2w p2_e13m p2_e12m p2_e2wks p1_e7m p1_e5m p1_e12m p1_e13m p3_e12m p3_e13m using "`sdvdata'"

	 drop   sdv_econactiv sdv_profits


	 gen validated = _merge == 3
	 drop _merge




	///Labeling SDV Variable///
	la var sdv_steal "SDV Steal"
	la var sdv_gambl "SDV Gamble"
	la var sdv_video "SDV Video"
	la var sdv_phone "SDV Phone"
	la var sdv_homeless "SDV Homeless"
	la var sdv_marij "SDV Marijuana"


  		// SDV construction //

		qui gen marij_sdv_e=.
		qui replace marij_sdv_e=0 if (druggrass7dx_e!=. | druggrassprev7dx_e!=. | druggrass7d_e!=. | druggrassprev7d_e!=.)
		qui replace marij_sdv_e=1 if (druggrass7dx_e>0 & druggrass7dx_e!=. | druggrassprev7dx_e>. & druggrassprev7dx_e!=. | druggrass7d_e>0 & druggrass7d_e!=. |  druggrassprev7d_e>0 & druggrassprev7d_e!=.)
		qui la var marij_sdv_e "Smoked marijuana in the past 2 weeks -- SDV comparison"

		qui gen gambl_sdv_e=.
		qui replace gambl_sdv_e=0 if (gamble7ddys_e!=. | gamble2wdys_e!=.)
		qui replace gambl_sdv_e=1 if (gamble7ddys_e>0 & gamble7ddys_e!=. | gamble2wdys_e>0 & gamble2wdys_e!=.)
		qui la var  gambl_sdv_e "Gambled in the past 2 weeks -- SDV comparison"

		qui gen steal_sdv_e=.
		qui replace steal_sdv_e=0 if (stealtakethings2w_e!=. | stealmistakecorr2w_e!=. | stealscrape2w_e!=. | stealtakethings4w_e!=. | stealmistakecorr4w_e!=. | stealscrape4w_e!=.)
		qui replace steal_sdv_e=1 if (stealtakethings2w_e==1 | stealmistakecorr2w_e==1 | stealscrape2w_e==1 | stealtakethings4w_e==1 | stealmistakecorr4w_e==1 | stealscrape4w_e==1)
		qui la var steal_sdv_e "Stole in the past 2 weeks -- SDV comparison"

		qui gen homeless_sdv_e=.
		qui replace homeless_sdv_e=0 if (sleepstreet2w_e==0 | sleepstreet4w_e==0)
		qui replace homeless_sdv_e=1 if (sleepstreet2w_e==1 | sleepstreet4w_e==1)
		qui la var homeless_sdv_e "Homeless in past 2 weeks -- SDV comparison"

		qui gen phone_sdv_e=.
		qui replace phone_sdv_e=0 if (phonecharge7dx_e!=. | phonecharge2wx_e!=.)
		qui replace phone_sdv_e=1 if (phonecharge7dx_e!=. & phonecharge7dx_e>0 | phonecharge2wx_e!=. & phonecharge2wx_e>0)
		qui la var phone_sdv_e "Charged phone in past 2 weeks -- SDV comparison"

		qui gen video_sdv_e=.
		qui replace video_sdv_e=0 if (videoclub7dx_e!=. | videoclub2wx_e!=.)
		qui replace video_sdv_e=1 if (videoclub7dx_e>0 & videoclub7dx_e!=. | videoclub2wx_e!=. & videoclub2wx_e>0)
		qui la var video_sdv_e "Went to video club in past 2 weeks -- SDV comparison"



	// Generating Additive Indices //
	egen sensitive_sdv_e = rowtotal (steal_sdv_e marij_sdv_e homeless_sdv_e gambl_sdv_e), m
		la var sensitive_sdv_e "Survey measures: sensitive topics (0-4)"
	egen nonsensitive_sdv_e = rowtotal (video_sdv_e phone_sdv_e), m
		la var nonsensitive_sdv_e "Survey measures: non-sensitive topics (0-2)"
	egen all_sdv_e = rowtotal (steal_sdv_e marij_sdv_e homeless_sdv_e gambl_sdv_e video_sdv_e phone_sdv_e), m
		la var all_sdv_e "Survey measures: all topics (0-6)"

	egen sdv_sensitive = rowtotal (sdv_steal sdv_marij sdv_homeless sdv_gambl ), m
		la var sdv_sensitive "SDV measures: sensitive topics (0-4)"
	egen sdv_nonsensitive = rowtotal (sdv_video sdv_phone), m
		la var sdv_nonsensitive "SDV measures: non-sensitive topics (0-2)"
	egen sdv_all = rowtotal (sdv_steal sdv_marij sdv_homeless sdv_gambl sdv_video sdv_phone), m
		la var sdv_all "SDV measures: all topics (0-6)"


	//Subtract SDV from survey//
	gen steal_sdv_diff = steal_sdv_e - sdv_steal if (sdv_steal!=. & steal_sdv_e!=.)
		la var steal_sdv_diff "Stealing  -- Survey measure less SDV measure)"
	gen gambl_sdv_diff = gambl_sdv_e - sdv_gambl if (sdv_gambl!=. & gambl_sdv_e!=.)
		la var gambl_sdv_diff "Gambling  -- Survey measure less SDV measure)"
	gen video_sdv_diff = video_sdv_e - sdv_video if (sdv_video!=. & video_sdv_e!=.)
		la var video_sdv_diff "Video Club  -- Survey measure less SDV measure)"
	gen phone_sdv_diff = phone_sdv_e - sdv_phone if (sdv_phone!=. & phone_sdv_e!=.)
		la var phone_sdv_diff "Phone Charging  -- Survey measure less SDV measure)"
	gen homeless_sdv_diff = homeless_sdv_e - sdv_homeless if (sdv_homeless!=. & homeless_sdv_e!=.)
		la var homeless_sdv_diff "Homelessness  -- Survey measure less SDV measure)"
	gen marij_sdv_diff = marij_sdv_e - sdv_marij if (sdv_marij!=. & marij_sdv_e!=.)
		la var marij_sdv_diff "Marijuana  -- Survey measure less SDV measure)"
	gen sensitive_sdv_diff = sensitive_sdv_e - sdv_sensitive if (sdv_sensitive!=. & sensitive_sdv_e!=.)
		la var sensitive_sdv_diff "Sensitive topics -- Survey index less SDV index"
	gen nonsensitive_sdv_diff = nonsensitive_sdv_e - sdv_nonsensitive if (sdv_nonsensitive!=. & nonsensitive_sdv_e!=.)
		la var nonsensitive_sdv_diff "Non-sensitive topics -- Survey index less SDV index"
	gen all_sdv_diff = all_sdv_e - sdv_all if (sdv_all!=. & all_sdv_e!=.)
		la var all_sdv_diff "All topics -- Survey index less SDV index"




foreach x in steal marij gambl homeless video phone sensitive nonsensitive {
	/* Set counter */
	local i = 0
	/* Label variables */
	la var sdv_`x' "SDV Measure"
	foreach t in tpassonly cashassonly tpcashass {
		qui gen `x'X`t' = sdv_`x'*`t'
	}
	la var `x'Xtpassonly "CBT Interaction"
	la var `x'Xcashassonly "Cash Interaction"
	la var `x'Xtpcashass "Both Interaction"


	/* R2's regression -- Block and Round FEs */

	local ++i
	local var`i' "R2s Regression, block and round fixed effects"
	local `x'_p`i' = 0
	/* Get DV mean */
	local ++i
	local var`i' "DV mean"
	qui sum `x'_sdv_e if !missing(sdv_`x')
	local `x'_b`i' = r(mean)
	local `x'_p`i' = 0

	qui svy: reg `x'_sdv_e sdv_`x' tpassonly cashassonly tpcashass ///
	`x'Xtpassonly `x'Xcashassonly `x'Xtpcashass ${cohorts}
		/* Recover parameters */
		foreach z in _cons sdv_`x' tpassonly cashassonly tpcashass ///
		`x'Xtpassonly `x'Xcashassonly `x'Xtpcashass {
			local ++i
				if "`z'" == "_cons" local var`i' "Constant"
				else local var`i': var la `z'
				local `x'_b`i' = _b[`z']
				local `x'_se`i' = _se[`z']
				qui test `z'
				local `x'_p`i' = r(p)
		}
	local ++i
	local var`i' "B0+B1"
	lincom _cons + sdv_`x'
		local `x'_b`i' = r(estimate)
		local `x'_se`i' = r(se)
		local `x'_p`i' = 2*ttail(r(df),r(estimate)/r(se))
	local ++i
	local var`i' "B2+B5"
	lincom tpassonly + `x'Xtpassonly
		local `x'_b`i' = r(estimate)
		local `x'_se`i' = r(se)
		local `x'_p`i' = 2*ttail(r(df),r(estimate)/r(se))
	local ++i
	local var`i' "B3+B6"
	lincom cashassonly + `x'Xcashassonly
		local `x'_b`i' = r(estimate)
		local `x'_se`i' = r(se)
		local `x'_p`i' = 2*ttail(r(df),r(estimate)/r(se))
	local ++i
	local var`i' "B4+B7"
	lincom tpcashass + `x'Xtpcashass
		local `x'_b`i' = r(estimate)
		local `x'_se`i' = r(se)
		local `x'_p`i' = 2*ttail(r(df),r(estimate)/r(se))



	/* Imposing our constraints */
	local ++i
	local var`i' "Imposing our constraints"
	local `x'_p`i' = 0
	/* Get DV mean */
	local ++i
	local var`i' "DV mean"
	qui sum `x'_sdv_e if !missing(sdv_`x')
	local `x'_b`i' = r(mean)
	local `x'_p`i' = 0
		/* Define Constraints */
		constraint 1 `x'Xtpassonly = 0
		constraint 2 `x'Xcashassonly = 0
		constraint 3 `x'Xtpcashass = 0
		constraint 4 sdv_`x' = 1
	qui svy: cnsreg `x'_sdv_e sdv_`x' tpassonly cashassonly tpcashass ///
	`x'Xtpassonly `x'Xcashassonly `x'Xtpcashass  ${cohorts}, c(1-4)
		/* Recover parameters */
		local us_`x'_n = e(N)
		foreach z in _cons sdv_`x' tpassonly cashassonly tpcashass ///
		`x'Xtpassonly `x'Xcashassonly `x'Xtpcashass {
			local ++i
				if "`z'" == "_cons" local var`i' "Constant"
				else local var`i': var la `z'
				local `x'_b`i' = _b[`z']
				local `x'_se`i' = _se[`z']
				qui test `z'
				local `x'_p`i' = r(p)
		}
	local I = `i'

	/* Format standard errors */
	forv i = 1/`I' {
			local se = round(``x'_se`i'',.001)
			/* Fix rounding (Sometimes it rounds 1.234123 to 1.234000000001) */
			local dec = ""
			local dec = substr("`se'",strpos("`se'","."),.)
			if (strlen("`dec'")>4) local se = substr("`se'",1,strpos("`se'",".")+3)
			if (strlen("`dec'")==3) local se = "`se'0"
			if (strlen("`dec'")==2) local se = "`se'00"
			if (strlen("`dec'")<2) local se = "`se'000"
			/* Add asterisks and brackets */
			local `x'`term'_se`i' = "[`se']"
			if (``x'_p`i''<=.10) local `x'_se`i' = "``x'_se`i''*"
			if (``x'_p`i''<=.05) local `x'_se`i' = "``x'_se`i''*"
			if (``x'_p`i''<=.01) local `x'_se`i' = "``x'_se`i''*"
	}
	drop `x'X*
}
preserve
	clear
	set obs `=`I'*2'
	gen var = ""
	forv i = 1/`I' {
		qui replace var = "`var`i''" in `=2*`i'-1'
	}
	foreach x in steal marij gambl homeless video phone sensitive nonsensitive {
		qui gen `x' = ""
		forv i = 1/`I' {
			qui replace `x' = "``x'_b`i''" in `=2*`i'-1'
			qui replace `x' = "``x'_se`i''" in `=2*`i''
		}
	}
	drop if steal == "[0000]***"
	export excel using "outfiles/AER2017.xlsx", sheetreplace sheet("t9 raw") firstrow(var)
restore
$exit
